package com.community.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.community.bean.SysDict;
import com.feng.fengDUtil.BaseService;
import com.community.util.Search;
import com.feng.fengDUtil.Page;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class DictService extends BaseService {

   private Map<Integer,Integer[]> thresholdMap = new HashMap<>();

    public void saveDict(SysDict dict) {
        saveOrUpdate(dict);
        initThresholdMap();
    }

    public void initThresholdMap(){
        String jsonStr = sql("select jsonStr from sys_dict where type = 'batchScore'").one(String.class);
        if(StringUtils.isNotEmpty(jsonStr)){
            JSONArray jsonArray = JSONArray.parseArray(jsonStr);
            for(int i= 0;i<jsonArray.size();i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Integer id =  jsonObject.getIntValue("id");
                Integer[] vals = getIntegers(jsonObject.getString("val"));
                if(vals ==null){
                    continue;
                }
                thresholdMap.put(id,vals);
            }
        }
    }


    public Integer[] getThreshold(Integer batch){
        Integer[] ret = thresholdMap.get(batch);
        if(ret!=null){
            return ret;
        }
        switch (batch){
            case 1:return new Integer[]{1500,60000};
            case 2:return new Integer[]{53000,156885};
            case 3:return new Integer[]{156886,234340};
        }
        return null;
    }

    private Integer[] getIntegers(String str){
        if(StringUtils.isEmpty(str)){
            return null;
        }
        String separator = null;
        if(str.indexOf(",")>0){
            separator = ",";
        }else if(str.indexOf("，")>0){
            separator = "，";
        }
        if(separator == null){
            return null;
        }
        String[] strs = str.split(separator);
        if(strs.length!=2){
            return null;
        }
        try {
            return new Integer[]{Integer.parseInt(strs[0]),Integer.parseInt(strs[1])};
        }catch (Exception e){
            return null;
        }
    }

    private static final Logger log = LoggerFactory.getLogger(DictService.class);

    public List<SysDict> getDict(String type) {
        return sql("select * from sys_dict where type = ? order by code ").list(SysDict.class);
    }

    public List<SysDict> allDict() {
        return selectAll(SysDict.class);
    }

    public Page query(Page page, Search search) {
        ArrayList params = new ArrayList();
        String sql = "select u.* from sys_dict u  where 1=1 ";
        if(StringUtils.isNotEmpty(search.getName())){
            sql+=" and u.name like ? ";
            params.add(processLike(search.getName()));
        }
        sql+=" order by u.type desc";
        findPageList(Map.class,sql,page,params.toArray());
        return page;
    }

    public JSONArray getBatchDates() {
        String jsonStr = sql("select jsonStr from sys_dict where type = 'batchDate'").one(String.class);
        return JSONArray.parseArray(jsonStr);
    }


}
